package com.realbrand.net.realbrandpay.ui.activity.wallet.set;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.realbrand.net.realbrandpay.R;
import com.realbrand.net.realbrandpay.api.HTTPAPI;
import com.realbrand.net.realbrandpay.base.BaseActivity;
import com.realbrand.net.realbrandpay.inter.IForgetPassword;
import com.realbrand.net.realbrandpay.inter.IForgetPayPasswd;
import com.realbrand.net.realbrandpay.ui.activity.login.LoginActivity;
import com.realbrand.net.realbrandpay.ui.activity.shopcar.CheckOutActivity;
import com.realbrand.net.realbrandpay.ui.activity.wallet.WalletActivity;
import com.realbrand.net.realbrandpay.ui.activity.wallet.withdraw.WithdrawActivity;
import com.realbrand.net.realbrandpay.untils.Static;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by Administrator on 2017/8/8.
 */

public class ForgetPayPasswordActivity extends BaseActivity {
    @BindView(R.id.forgetPassword_toolbar)
    Toolbar forgetPasswordToolbar;
    @BindView(R.id.et_forgetActivity_password)
    EditText etForgetActivityPassword;
    @BindView(R.id.et_forgetActivity_passwordAgain)
    EditText etForgetActivityPasswordAgain;
    @BindView(R.id.bt_forgetActivity_sure)
    Button btForgetActivitySure;

    //用户手机号码
    private String mobile;
    //用户两次输入的密码
    private String password, passwordAgain;

    private String encode;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        mobile = getIntent().getStringExtra("mobile");
        forgetPasswordToolbar.setTitle("");
        forgetPasswordToolbar.setNavigationIcon(R.mipmap.ic_back);
        setSupportActionBar(forgetPasswordToolbar);
        forgetPasswordToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setOnClick();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.forgetpaypasswd;
    }

    @Override
    protected void setOnClick() {
        btForgetActivitySure.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_forgetActivity_sure:
                //先获取用户两次输入的密码，判断两次输入的密码是否一致
                password = etForgetActivityPassword.getText().toString();
                passwordAgain = etForgetActivityPasswordAgain.getText().toString();
                if (TextUtils.isEmpty(password) && TextUtils.isEmpty(passwordAgain)) {
                    Static.toast(this, "请输入你要设置的密码");
                } else {
                        if (password.equals(passwordAgain)) {
                            //做请求
                            forget();
                        } else {
                            Static.toast(this, "请确定两次输入的密码保持一致");
                        }
                }
                break;
        }
    }

    private void forget() {
        JSONObject jsonObject = new JSONObject();
        ArrayList<String> keyList = new ArrayList<>();
        keyList.add("UserMobile");
        keyList.add("PayPasswd");
        ArrayList<String> valueList = new ArrayList<>();
        valueList.add(mobile);
        valueList.add(password);
        String string = Static.getString(keyList, valueList);
        try {
            encode = URLEncoder.encode(string, "utf-8");
            try {
                jsonObject.put("SOURCE", "ForgetPayPasswordActivity");
                jsonObject.put("CREDENTIALS", "0");
                jsonObject.put("TERMINAL", "1");
                jsonObject.put("INDEX", Static.getDay());
                jsonObject.put("METHOD", "ForgetPayPasswd");
                jsonObject.put("DATA", encode);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(HTTPAPI.JSON, jsonObject.toString());
        Retrofit retrofit = new Retrofit.Builder().baseUrl(HTTPAPI.BASE_URL).build();
        IForgetPayPasswd iForgetPassword = retrofit.create(IForgetPayPasswd.class);
        Call<ResponseBody> call = iForgetPassword.forgetPassword(body);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response == null) {
                    Static.toast(ForgetPayPasswordActivity.this, "网络繁忙");
                } else {
                    try {
                        String result = response.body().string();
                        Log.d("zhu", "忘记密码界面返回的结果" + result.toString());
                        try {
                            JSONObject json = new JSONObject(result);
                            String data = json.getJSONArray("DATA").getJSONObject(0).getString("result");
                            if ("0".equals(data)) {
                                Static.toast(ForgetPayPasswordActivity.this, "修改失败");
                            } else if ("1".equals(data)) {
                                finish();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }
}
